package com.mes.web.controller.biz;

import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.Map;

import com.mes.biz.domain.*;
import com.mes.biz.domain.vo.OrderVo;
import com.mes.biz.domain.vo.ProcessCompletionVo;
import com.mes.biz.service.BizProductProcessPlanService;
import com.mes.biz.service.BizProductService;
import com.mes.common.annotation.Log;
import com.mes.common.core.domain.AjaxResult;
import com.mes.common.core.domain.BaseEntity;
import com.mes.common.core.domain.R;
import com.github.pagehelper.PageInfo;
import com.mes.common.core.domain.model.LoginUser;
import com.mes.common.core.page.PageDomain;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.mes.common.enums.BusinessType;
import com.mes.common.utils.poi.ExcelUtil;
import com.mes.framework.cache.DictUtils;
import javax.validation.constraints.*;

import com.mes.framework.web.service.TokenService;
import org.springframework.web.bind.annotation.*;
import com.mes.common.core.controller.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import com.mes.biz.service.BizOrderProductService;

/**
 * 销售单产品Controller
 * @author hwg
 * @email hwg
 * @date 2023-05-15
 */
@RestController
    @RequestMapping("/biz/bizOrderProduct")
public class BizOrderProductController extends BaseController {

    @Autowired
    private BizOrderProductService bizOrderProductService;
    @Autowired
    private BizProductService bizProductService;
    @Autowired
    private TokenService tokenService;

    /**
     * 查询销售单产品列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizOrderProduct:list')")
    @GetMapping("/list")
    public R<PageInfo> list(BizOrderProduct bizOrderProduct, HttpServletRequest request, HttpServletResponse response) {
        bizOrderProduct.setPage(new PageDomain(request, response));
        PageInfo<BizOrderProduct> page = bizOrderProductService.findPage(bizOrderProduct);
//        for (int i = 0; i < page.getList().size(); i++) {
//            BizProduct bizProduct = bizProductService.get(page.getList().get(i).getProductId());
//            page.getList().get(i).setProductCode(bizProduct.getProductCode());
//            page.getList().get(i).setModel(bizProduct.getModel());
//            page.getList().get(i).setBrand(bizProduct.getBrand());
//            page.getList().get(i).setColor(bizProduct.getColor());
//            page.getList().get(i).setSpec(bizProduct.getSpec());
//            page.getList().get(i).setUnitA(bizProduct.getUnit());
//            page.getList().get(i).setUnitB(bizProduct.getAssistUnitA());
//            page.getList().get(i).setUnitC(bizProduct.getAssistUnitB());
//        }
        return R.data(page);
    }

    /**
     * 获取销售单产品详细信息
     */
    @PreAuthorize("@ss.hasPermi('biz:bizOrderProduct:query')")
    @GetMapping(value = "/{id}")
    public R<BizOrderProduct> detail(@PathVariable("id") String id) {
        return R.data(bizOrderProductService.get(id));
    }

    /**
     * 新增销售单产品
     */
    @PreAuthorize("@ss.hasPermi('biz:bizOrderProduct:add')")
    @Log(title = "销售单产品", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated  BizOrderProduct bizOrderProduct) {
        return R.status(bizOrderProductService.save(bizOrderProduct));
    }

    /**
     * 修改销售单产品
     */
    @PreAuthorize("@ss.hasPermi('biz:bizOrderProduct:edit')")
    @Log(title = "销售单产品", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated BizOrderProduct bizOrderProduct) {
        return R.status(bizOrderProductService.save(bizOrderProduct));
    }

    /**
     * 删除销售单产品
     */
    @PreAuthorize("@ss.hasPermi('biz:bizOrderProduct:remove')")
    @Log(title = "销售单产品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids) {
        return R.status(bizOrderProductService.deleteBizOrderProductByIds(ids));
    }


    /**
     * 导出销售单产品列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizOrderProduct:export')")
    @Log(title = "销售单产品", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(BizOrderProduct bizOrderProduct) {
        List<BizOrderProduct> list = bizOrderProductService.findList(bizOrderProduct);
        ExcelUtil<BizOrderProduct> util = new ExcelUtil<BizOrderProduct>(BizOrderProduct.class);
        return util.exportExcel(list, "销售单产品数据");
    }

    /**
     * 根据字典类型查询字典数据信息等其他自定义信息
     */
    @GetMapping(value = "/getInitData/{dictTypes}")
    public R getInitData(@PathVariable String dictTypes) {
        Map<String, Object> dataMap = new HashMap<String, Object>();
        dataMap.putAll(DictUtils.getMultiDictList(dictTypes));
        return R.data(dataMap);
    }

    /**
     * @Author: wy
     * @Description:工序交接-生产商品明细
     * @Params:
     * @Return
     */
    @GetMapping("/getProductDetailList")
    public R getProcessDetailList(BizProductionOrder bizProductionOrder, HttpServletRequest request, HttpServletResponse response) {
        LoginUser loginUser = tokenService.getLoginUser(request);
        List<BizOrderProduct> list = bizOrderProductService.getProcessDetailList(bizProductionOrder.getProductOrderId(),loginUser.getUserId(),bizProductionOrder.getPlanId());
        return R.data(list);
    }

    /**
    * @Author: wy
    * @Description: 下达生产列表
    * @DateTime: 2023-07-13 14:41
    * @Params:
    * @Return
    */
    @PreAuthorize("@ss.hasPermi('biz:bizOrderProduct:query')")
    @GetMapping(value = "/detailList/{orderId}")
    public AjaxResult detailList(@PathVariable("orderId") String orderId) {
        return AjaxResult.success(bizOrderProductService.detailList(orderId));
    }


    /**
     * 新增销售单产品
     */
    @Log(title = "销售单产品", businessType = BusinessType.INSERT)
    @PostMapping("updateProcess")
    public R updateProcess(@RequestBody List<BizOrderProduct> bizOrderProduct) {
        return R.status(bizOrderProductService.updateProcess(bizOrderProduct));
    }

}
